package cn.dyw.cloudmusic.http;

import lombok.Getter;
import lombok.Setter;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author: dyw770
 * @Date: 2018/12/11 20:56
 * @Description: 网易云音乐爬取
 */
public class CloudMusicHttpRequest {

    @Getter
    @Setter
    private OkHttpClient okHttpClient;

    public CloudMusicHttpRequest(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
    }

    public void startCall(SaveHandle saveHandle, InitMusicParams musicParams) {
        Request.Builder builder = new Request.Builder();
        HttpHeaderBuild.buildHttpHeader(builder, musicParams.getParams());
        Request request = null;
        try {
            request = builder.url(musicParams.createRequestUrl())
                    .post(musicParams.buildRequestBody())
                    .build();
            Response execute = okHttpClient.newCall(request).execute();
            saveHandle.save(execute, musicParams.getParams());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
